<?

require_once("cms/core/helper/Route.php");
require_once("cms/menu/om/MenuPeer.php");

class PHP_CMS_View_Helper_MenuRoute extends PHP_CMS_View_Helper_Route {

    public function menuRoute($parentId) {

        $route = array();
        $skip = true;

        if($parentId > 0) {

            while($parentId > 0) {

                $sql = "SELECT menu_item_i18n.name, menu_item.parent_id, menu_item.menu_id FROM menu_item_i18n " .
                       "LEFT JOIN menu_item USING (menu_item_id) " .
                       "LEFT JOIN menu USING (menu_id) " .
                       "WHERE menu_item_i18n.language_code = menu.language_code " .
                       "AND menu_item_i18n.menu_item_id = ". $parentId . " " .
                       "LIMIT 1";

                $conn = Propel::getConnection();
                $stmt = $conn->createStatement();
                $rs   = $stmt->executeQuery($sql, ResultSet::FETCHMODE_NUM);

                if($rs->next()) {

                    $parentId = $rs->getInt(2) === null ?
                        0 : $rs->getInt(2);

                    if(!$skip) {
                        $route[] = array(
                            "name"=>$rs->getString(1),
                            "href"=>"menu/item/index?parentId=" . $parentId
                        );
                    }

                    if($parentId == 0) {

                        $sql = "SELECT menu.name, menu.menu_id FROM menu " .
                               "WHERE menu.menu_id = ". $rs->getInt(3) . " " .
                               "LIMIT 1";


                        $rs = $stmt->executeQuery($sql, ResultSet::FETCHMODE_NUM);
                        if($rs->next()) {
                            $route[] = array(
                                "name"=>$rs->getString(1),
                                "href"=>"menu/item/index?id=".$rs->getInt(2)."&amp;parentId=0"
                            );
                        }

                   }

                   $skip = false;

                } else {
                    $parentId = 0   ;
                }

            }

            $route = array_reverse($route);

        }

        return $route;


    }

}

?>